import java.util.*;

public class Solution {

    //

    List<List<Integer>> ret;
    List<Integer> path;
    boolean[] check;

    public List<List<Integer>> permute(int[] nums) {
        ret = new ArrayList<>();
        path = new ArrayList<>();
        check = new boolean[nums.length];

        dfs(nums);
        return ret;
    }

    public void dfs(int[] nums){
        if(nums.length == path.size()){
            ret.add(new ArrayList<>(path));
            return ;
        }

        for(int i = 0; i < nums.length; i++){
            if(check[i] == false){
                path.add(nums[i]);
                check[i] = true;

                dfs(nums);

                check[i] = false;
                path.remove(path.size() - 1);
            }
        }
    }

    //
//    List<List<Integer>> ret;
//    List<Integer> path;

    public List<List<Integer>> subsets(int[] nums) {
        ret = new ArrayList<>();
        path = new ArrayList<>();

        dfs(nums, 0);

        return ret;
    }

    public void dfs(int[] nums, int pos){
        if(nums.length == pos){
            ret.add(new ArrayList<>(path));
            return ;
        }

        path.add(nums[pos]);
        dfs(nums, pos + 1);
        path.remove(path.size() - 1);

        dfs(nums, pos + 1);
    }

    public void dfs2(int[] nums, int pos){
        ret.add(new ArrayList<>(path));

        for(int i = pos; i < nums.length; i++){
            path.add(nums[i]);
            dfs(nums, i + 1);
            path.remove(path.size() - 1);
        }
    }

    //
    int sum;
    int path;

    public int subsetXORSum(int[] nums) {
        dfs(nums, 0);
        return sum;
    }

    public void dfs(int[] nums, int pos){
        sum += path;

        for(int i = pos; i < nums.length; i++){
            path ^= nums[i];
            dfs(nums, i + 1);
            path ^= nums[i];
        }
    }

    //
    List<List<Integer>> ret;
    List<Integer> path;
    boolean[] check;

    public List<List<Integer>> permuteUnique(int[] nums) {
        ret = new ArrayList<>();
        path = new ArrayList<>();
        check = new boolean[nums.length];

        Arrays.sort(nums);
        dfs(nums, 0);
        return ret;
    }

    public void dfs(int[] nums, int pos) {
        if (pos == nums.length) {
            ret.add(new ArrayList<>(path));
        }

        for (int i = 0; i < nums.length; i++) {
            //剪枝
            if (check[i] == true || (i != 0 && nums[i] == nums[i - 1] && check[i - 1] == false)) {
                continue;
            }
            path.add(nums[i]);
            check[i] = true;
            dfs(nums, pos + 1);

            //恢复现场
            path.remove(path.size() - 1);
            check[i] = false;
        }
    }

    public void dfs2(int[] nums, int pos){
        if(pos == nums.length){
            ret.add(new ArrayList<>(path));
        }

        for(int i = 0; i < nums.length; i++){
            //剪枝
            if(check[i] == false && (i == 0 || nums[i] != nums[i - 1] || check[i - 1] != false)){

                path.add(nums[i]);
                check[i] = true;
                dfs(nums, pos + 1);

                //恢复现场
                path.remove(path.size() - 1);
                check[i] = false;
            }
        }
    }
}
